package com.muqi.app.qlearn.fragments;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.MContants;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ScreenUtils;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.adapter.ClassRoomCommentStatisticsAdapter;
import com.muqi.app.qlearn.modles.Banner;
import com.muqi.app.qlearn.modles.LastRecord;
import com.muqi.app.qlearn.modles.StatisticsListInfo;
import com.muqi.app.qlearn.student.BaseFragment;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.ui.SetGroupInfoActivity;
import com.muqi.app.user.widget.SelectableRoundedImageView;

@SuppressLint("InflateParams")
public class BannerFragment extends BaseFragment {
	public static final String BANNER_HOME_CHANGED = "home_changed";

	public static final String BANNER_COMMENT_CHANGED = "comment_changed";
	public static final String BANNER_EXAMINATION_CHANGED = "examination_changed";
	private int resId = R.layout.fragment_banner_home;
	private int uiType;
	private SelectableRoundedImageView mSiv;
	private ImageView mivLeft;
	private ImageView miv;
	private TextView mtv;

	private String recordType;

	private BannerChanged bannerChanged;
	private LinearLayout mll;
	private LinearLayout mroot;
	private ImageView[] rbs = new ImageView[6];

	private LinearLayout.LayoutParams params;
	private int mH;
	private int mW;

	public BannerFragment(int resId, int uiType) {
		super();
		this.resId = resId;
		this.uiType = uiType;
	}

	public BannerFragment(int resId) {
		super();
		this.resId = resId;
	}

	public BannerFragment() {
		super();
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		bannerChanged = new BannerChanged();
		IntentFilter filter = new IntentFilter();
		filter.addAction(MContants.REFRESH_FILETREE_ACTION);
		filter.addAction(BANNER_COMMENT_CHANGED);
		filter.addAction(BANNER_HOME_CHANGED);
		filter.addAction(MContants.CHANGE_CURRENT_CHILD_ACTION);
		filter.addAction(BANNER_EXAMINATION_CHANGED);
		getActivity().registerReceiver(bannerChanged, filter);
	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(resId, null);
		initView(view);

		return view;
	}

	private void initView(View view) {
		mH = (int) Math.ceil(ScreenUtils.getScreenH(mActivity)
				* (4 / (double) 6));
		mW = (int) Math.ceil(ScreenUtils.getScreenW(mActivity)
				* (1 * (double) 2));
		switch (resId) {
		case R.layout.fragment_banner_home:
			 //mroot=(LinearLayout) view.findViewById(R.id.linearLayout1);
			
			miv = (ImageView) view.findViewById(R.id.banner_iv);
			mtv = (TextView) view.findViewById(R.id.banner_tv_des);
			mSiv = (SelectableRoundedImageView) view
					.findViewById(R.id.banner_pic);
			mivLeft = (ImageView) view.findViewById(R.id.banner_iv_left);
			mll = (LinearLayout) view.findViewById(R.id.banner_comment);
		   
			rbs[0] = (ImageView) view.findViewById(R.id.banner_hand);
			rbs[1] = (ImageView) view.findViewById(R.id.banner_listener);
			rbs[2] = (ImageView) view.findViewById(R.id.banner_wk);
			rbs[3] = (ImageView) view.findViewById(R.id.banner_help);
			rbs[4] = (ImageView) view.findViewById(R.id.banner_think);
			rbs[5] = (ImageView) view.findViewById(R.id.banner_do);
			if (uiType == Banner.home.ordinal()) {

				if (mSpUtil.getNotification(mSpUtil.getCurrentChildId() + "h") == 0
						&& mSpUtil.getNotification(mSpUtil.getCurrentChildId()
								+ "n") == 0) {
					mivLeft.setVisibility(View.VISIBLE);
					miv.setVisibility(View.GONE);
					mivLeft.setImageResource(R.drawable.banner_home);
					mtv.setText("还没有收到新的作业和通知");
				} else {
					mivLeft.setVisibility(View.GONE);
					miv.setVisibility(View.VISIBLE);
					miv.setImageResource(R.drawable.banner_has_hw);
					if (mSpUtil.getNotification(mSpUtil.getCurrentChildId()
							+ "h") != 0
							&& mSpUtil.getNotification(mSpUtil
									.getCurrentChildId() + "n") != 0) {
						mtv.setText("有"
								+ mSpUtil.getNotification(mSpUtil
										.getCurrentChildId() + "h")
								+ "条新作业和"
								+ mSpUtil.getNotification(mSpUtil
										.getCurrentChildId() + "n") + "条新通知");
					} else if (mSpUtil.getNotification(mSpUtil
							.getCurrentChildId() + "h") != 0) {
						mtv.setText("有"
								+ mSpUtil.getNotification(mSpUtil
										.getCurrentChildId() + "h") + "条新作业");
					} else {
						mtv.setText("有"
								+ mSpUtil.getNotification(mSpUtil
										.getCurrentChildId() + "h") + "条新通知");
					}

				}

			} else if (uiType == Banner.vedio.ordinal()) {
				miv.setImageResource(R.drawable.banner_vedio);
				mtv.setText("");
			} else if (uiType == Banner.society.ordinal()) {
				recordType = "F5";
				loadRecord("F5");
			} else if (uiType == Banner.art.ordinal()) {
				recordType = "F4";
				loadRecord("F4");
			} else if (uiType == Banner.think.ordinal()) {
				recordType = "F1";
				loadRecord("F1");
			} else if (uiType == Banner.comment.ordinal()) {
			
				loadCommentData();
				
			} else if(uiType==Banner.acad.ordinal()){
				if (mSpUtil.getNotification(mSpUtil.getCurrentChildId()
						+ "e") == 0) {
					mtv.setText("还没有任何试题回复");
					mivLeft.setVisibility(View.GONE);
					miv.setVisibility(View.VISIBLE);
					miv.setImageResource(R.drawable.banner_study);
				} else {
					mivLeft.setVisibility(View.GONE);
					miv.setVisibility(View.VISIBLE);
					miv.setImageResource(R.drawable.banner_study);
					mtv.setText("有"
							+ mSpUtil.getNotification(mSpUtil
									.getCurrentChildId() + "e")
							+ "条试题回复");

				}
			}
				break;

		default:
			break;
		}

	}

	/**
	 * 加载最新的课堂评价
	 */
	private void loadCommentData() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("student_id", mSpUtil.getCurrentChildId());
		sendMap.put("dateTime", "today");
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.GET_LAST_COMMENT_API, sendMap);
		MyAsyncHttp.get(getActivity(), requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				if (resuleCode == RESULTCODE.SUCCESS) {

					if (resuleCode == RESULTCODE.SUCCESS) {
						try {
							JSONObject obj = new JSONObject(result);
							JSONObject objData = obj.getJSONObject("data");
							String dataCount = objData.getString("dataCount");
							showComment(dataCount);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						/*
						 * ArrayList<StatisticsListInfo> temp = ResponseUtils
						 * .getStatisticsListInfos(mActivity, result); if (temp
						 * != null) { showComment(temp); }
						 */

					}

				} else {
					ShowToast.showShort(mActivity, result);
				}
			}

		});

	}

	/**
	 * 根据dataCount展示数据
	 * 
	 * @param dataCount
	 */
	protected void showComment(String dataCount) {
		if (dataCount != null && !"".equals(dataCount)) {
			mll.setVisibility(View.VISIBLE);
			miv.setVisibility(View.GONE);
			mivLeft.setVisibility(View.GONE);
			mtv.setVisibility(View.GONE);
			String[] strs = dataCount.split(",");
			for (int i = 0; i < strs.length; i++) {
				if (strs[i].equals("0")) {
					rbs[i].setEnabled(true);
				} else {
					rbs[i].setEnabled(false);
				}
			}
		}else{
			miv.setImageResource(R.drawable.banner_comment);
			mtv.setText("还没有收到任何课堂评价");
		}

	}

	/**
	 * 展示评价的数据
	 * 
	 * @param temp
	 */
	protected void showComment(ArrayList<StatisticsListInfo> temp) {
		for (StatisticsListInfo statisticsListInfo : temp) {
			mll.setVisibility(View.VISIBLE);
			miv.setVisibility(View.GONE);
			mivLeft.setVisibility(View.GONE);
			mtv.setVisibility(View.GONE);
			if ("勇于举手发言".equals(statisticsListInfo.title)) {
				rbs[0].setEnabled(false);
			} else if ("上课认真听讲".equals(statisticsListInfo.title)) {
				rbs[1].setEnabled(false);
			} else if ("及时作业完成".equals(statisticsListInfo.title)) {
				rbs[2].setEnabled(false);
			} else if ("乐于帮助同学".equals(statisticsListInfo.title)) {
				rbs[3].setEnabled(false);
			} else if ("善于独立思考".equals(statisticsListInfo.title)) {
				rbs[4].setEnabled(false);
			} else if ("动手能力强".equals(statisticsListInfo.title)) {
				rbs[5].setEnabled(false);
			}
		}

	}

	/**
	 * 加载对应的最新一条的数据
	 * 
	 * @param string
	 */
	private void loadRecord(String string) {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("class_manager_id", mSpUtil.getChildClassId());
		sendMap.put("student_id", mSpUtil.getCurrentChildId());
		sendMap.put("type", string);
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.GET_LAST_GROWRECORD_API, sendMap);
		MyAsyncHttp.get(getActivity(), requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {

				if (resuleCode == RESULTCODE.SUCCESS) {

					LastRecord info = ResponseUtils.getLastTreeinfo(mActivity,
							result);

					showTree(info);

				} else {
					ShowToast.showShort(mActivity, result);
				}
			}

		});

	}

	/**
	 * 显示最新一条的记录
	 * 
	 * @param info
	 */
	protected void showTree(LastRecord info) {
		// TODO Auto-generated method stub
		if (info != null) {
			if (info.picInfo != null && info.picInfo.pic != null) {
				mSiv.setVisibility(View.VISIBLE);
				miv.setVisibility(View.GONE);
				GlideImageUtils.setImageLoader(mActivity,
						info.picInfo.pic.fileurl_02, mSiv,
						R.color.tree_content_bg_color);

			}
			mtv.setText(info.content);
		} else {
			mSiv.setVisibility(View.GONE);
			miv.setVisibility(View.VISIBLE);
			miv.setImageResource(R.drawable.banner_activity);
			mtv.setText("没有相关的记录");
		}

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		getActivity().unregisterReceiver(bannerChanged);
	}

	private class BannerChanged extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			if (arg1.getAction().equals(MContants.REFRESH_FILETREE_ACTION)) {
				if (recordType != null) {
					loadRecord(recordType);
				}

			} else if (arg1.getAction().equals(BANNER_COMMENT_CHANGED)) {
				if (uiType == Banner.comment.ordinal()) {
					loadCommentData();

				}

			} else if (arg1.getAction().equals(BANNER_HOME_CHANGED)) {
				if (uiType == Banner.home.ordinal()) {
					if (mSpUtil.getNotification(mSpUtil.getCurrentChildId()
							+ "h") == 0
							&& mSpUtil.getNotification(mSpUtil
									.getCurrentChildId() + "n") == 0) {
						mtv.setText("还没有收到新的作业和通知");
						mivLeft.setVisibility(View.VISIBLE);
						miv.setVisibility(View.GONE);
						mivLeft.setImageResource(R.drawable.banner_home);
					} else {
						mivLeft.setVisibility(View.GONE);
						miv.setVisibility(View.VISIBLE);
						miv.setImageResource(R.drawable.banner_has_hw);
						if (mSpUtil.getNotification(mSpUtil.getCurrentChildId()
								+ "h") != 0
								&& mSpUtil.getNotification(mSpUtil
										.getCurrentChildId() + "n") != 0) {
							mtv.setText("有"
									+ mSpUtil.getNotification(mSpUtil
											.getCurrentChildId() + "h")
									+ "条新作业和"
									+ mSpUtil.getNotification(mSpUtil
											.getCurrentChildId() + "n")
									+ "条新通知");
						} else if (mSpUtil.getNotification(mSpUtil
								.getCurrentChildId() + "h") != 0) {
							mtv.setText("有"
									+ mSpUtil.getNotification(mSpUtil
											.getCurrentChildId() + "h")
									+ "条新作业");
						} else {
							mtv.setText("有"
									+ mSpUtil.getNotification(mSpUtil
											.getCurrentChildId() + "h")
									+ "条新通知");
						}

					}
				}
			}else if(arg1.getAction().equals(MContants.CHANGE_CURRENT_CHILD_ACTION)){
				if (recordType != null) {
					loadRecord(recordType);
				}
				if (uiType == Banner.comment.ordinal()) {
					loadCommentData();

				}
			}else if(arg1.getAction().equals(BANNER_EXAMINATION_CHANGED)){
				if(uiType==Banner.acad.ordinal()){
					if (mSpUtil.getNotification(mSpUtil.getCurrentChildId()
							+ "e") == 0) {
						mtv.setText("还没有任何试题回复");
						mivLeft.setVisibility(View.GONE);
						miv.setVisibility(View.VISIBLE);
						miv.setImageResource(R.drawable.banner_study);
					} else {
						mivLeft.setVisibility(View.GONE);
						miv.setVisibility(View.VISIBLE);
						miv.setImageResource(R.drawable.banner_study);
						mtv.setText("有"
								+ mSpUtil.getNotification(mSpUtil
										.getCurrentChildId() + "e")
								+ "条试题回复");

					}
				}
				
			}

		}
	}
}
